package com.ihmp.dao;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ihmp.model.TCurrency;


@Repository("currencyDAO")
public class CCurrencyDAO implements ICurrencyDAO{
	@Autowired
	private SessionFactory sessionFactory;
	
	public void saveOrUpdate(TCurrency currency) {		
		sessionFactory.getCurrentSession().saveOrUpdate(currency);
	}
	
	public void delete(TCurrency currency) {		
		sessionFactory.getCurrentSession().delete(currency);
	}

	public TCurrency get(Integer id) {		
		return (TCurrency) sessionFactory.getCurrentSession().get(TCurrency.class, id);
		
	}

	@SuppressWarnings("unchecked")
	public List<TCurrency> list() {		
		Criteria crit = sessionFactory.getCurrentSession().createCriteria(TCurrency.class);
		crit.addOrder(Order.desc("createDate"));
		crit.addOrder(Order.desc("editDate"));
		return crit.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<TCurrency> list(int page,int recordPerPage) {		
		Criteria crit = sessionFactory.getCurrentSession().createCriteria(TCurrency.class);
		crit.setFirstResult((page-1)*recordPerPage).setMaxResults(page*recordPerPage -1);
		crit.addOrder(Order.desc("createDate"));
		crit.addOrder(Order.desc("editDate"));
		return crit.list();
	}
	
	public LinkedHashMap<Integer, String> getComboData(){
		LinkedHashMap<Integer, String> map = new LinkedHashMap<Integer, String>();
		ArrayList<TCurrency> currencyList = (ArrayList<TCurrency>) list();
		for(TCurrency currency : currencyList){
			map.put(currency.getId(), currency.getName());
		}
		return map;
	}
}
